import { requestClient } from '#/api/request';

export namespace DataCenterApi {
  /** 统计查询参数 */
  export interface StatisticsParams {
    page?: number;
    per_page?: number;
    start_date: string;
    end_date: string;
    landing_company_id?: number;
  }

  /** 自由职业者统计查询参数 */
  export interface FreelancerStatisticsParams extends StatisticsParams {
    freelancer_name?: string;
    freelancer_phone?: string;
    freelancer_id_card?: string;
  }

  /** 落地公司统计记录 */
  export interface CompanyStatisticsRecord {
    date: string;
    landing_company: string;
    landing_company_id: number;
    success_count: number;
    total_amount: string;
    total_commission: string;
    avg_fee_rate: string;
    total_service_fee: string;
    refund_amount: string;
  }

  /** 自由职业者统计记录 */
  export interface FreelancerStatisticsRecord {
    date: string;
    freelancer_name: string;
    freelancer_phone: string;
    freelancer_id_card: string;
    success_count: number;
    total_amount: string;
    total_commission: string;
    avg_fee_rate: string;
    total_service_fee: string;
    refund_amount: string;
  }

  /** 分页响应结构 */
  export interface StatisticsListResponse<T> {
    data: T[];
    current_page: number;
    per_page: number;
    total: number;
    last_page: number;
    from: number | null;
    to: number | null;
    first_page_url: string | null;
    last_page_url: string | null;
    next_page_url: string | null;
    prev_page_url: string | null;
    path: string | null;
    links: Array<{
      url: string | null;
      label: string;
      active: boolean;
    }>;
  }

  /** API响应包装 */
  export interface ApiResponse<T = any> {
    success: boolean;
    data: T;
    message?: string;
    error?: any;
  }
}

/**
 * 获取落地公司统计数据
 * @param params 查询参数
 */
export async function getCompanyStatisticsApi(params: DataCenterApi.StatisticsParams) {
  return requestClient.get<DataCenterApi.StatisticsListResponse<DataCenterApi.CompanyStatisticsRecord>>('/api/tenant/data-center/settlement-by-company', {
    params,
  });
}

/**
 * 获取自由职业者统计数据
 * @param params 查询参数
 */
export async function getFreelancerStatisticsApi(params: DataCenterApi.FreelancerStatisticsParams) {
  return requestClient.get<DataCenterApi.StatisticsListResponse<DataCenterApi.FreelancerStatisticsRecord>>('/api/tenant/data-center/settlement-by-freelancer', {
    params,
  });
}

/**
 * 导出落地公司统计数据
 * @param params 查询参数
 */
export async function exportCompanyDataApi(params: DataCenterApi.StatisticsParams) {
  return requestClient.get<any>('/api/tenant/data-center/export-company-data', {
    params,
  });
}

/**
 * 导出自由职业者统计数据
 * @param params 查询参数
 */
export async function exportFreelancerDataApi(params: DataCenterApi.FreelancerStatisticsParams) {
  return requestClient.get<any>('/api/tenant/data-center/export-freelancer-data', {
    params,
  });
}